This page details any known limitations and work-arounds of SevenMock.

SevenMock listeners need to extend concrete superclasses

This is because the listeners must override a single method only (to tell the mock which method to expect). This leads to the following problems:

  • When writing a test, a concrete class might not exist - if coding against an interface, for example. In fact, it is good practice to use Java interfaces to define APIs or services and to code and test against interfaces rather than concrete classes. This helps to decouple units of code from other units and from the tests. The work-around for this is to create a concrete adapter classes for the interfaces (see below).
  • It is not possible to use SevenMock to mock a final class or a final operation of a class, as it relies on the ability of a test to override operations. Instead of removing the 'final' statements, a better solution is to create an interface (if one doesn't already exist) and an adapter class - with both the adapter class and the collaborator implementing the interface.
  • When the listener is constructed, the superclass is also constructed. If there is no suitable no-parameter constructor on the superclass, the listener will have to pass parameters. Alternatively, a protected no-parameter constructor can be created on the superclass.
  • If the system is broken into components/modules, the concrete class is likely to exist in or depend on another module - but since we are intending to mock out this module, it would be nice not to have a dependency on it.
  • If the concrete class is a connector, these typically have a build-time dependency on both the client and server modules. If the test is par of the client module and dependent on the connector (e.g. because the listener extends the connector), then this can introduce a build-time circular dependency.

The obvious (although slightly unpleasant) solution to this problem is to create an adapter class that is a trivial implementation of the required interface. This would exist in the test package and only be used by the tests. Modern IDEs are able to generate these quickly. The annonymous inner class listener would then extend the adapter rather than directly implementing the interface. See SevenMock test in the distribution archive for an example - the interface IDependency1 is implemented by an adapter class called Dependency1 that is only used by the test. A real implementation of Dependency1 might not even exist (and in this case doesn't).


SevenMock Home

SourceForge.net Logo